package com.lagou.server;

import com.lagou.client.ScannerUtil;
import com.lagou.model.Question;
import com.lagou.model.QuestionScore;
import com.lagou.model.Student;
import com.lagou.model.User;

import java.io.*;
import java.util.LinkedHashMap;

/**
 * 实现数据的存取
 */
public class ServerDao {
    /**
     * 自定义成员变量，实现学生信息的存取
     * @param user
     * @return
     */
    private LinkedHashMap<Integer, Student> stuMap;
    private LinkedHashMap<Integer, Question> queMap;
    private static final String FILE_PATH = "d:/StudentInfo.txt";
    private static final String FILE_PATH1 = "d:/QuestionInfo.txt";
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    private QuestionScore qs = new QuestionScore();

    /**
     * 通过构造方法，实现学生信息及考试问题的初始化
     */
    public ServerDao() {
        File file = new File(FILE_PATH);
        if (0 == file.length()) {
            stuMap = new LinkedHashMap<>();
            Student stu1 = new Student(1001, "吕小布", "123456");
            Student stu2 = new Student(1002, "孙小权", "123456");
            Student stu3 = new Student(1003, "关小羽", "123456");
            Student stu4 = new Student(1004, "张小飞", "123456");
            stuMap.put(stu1.getId(), stu1);
            stuMap.put(stu2.getId(), stu2);
            stuMap.put(stu3.getId(), stu3);
            stuMap.put(stu4.getId(), stu4);
            writeObject();
        } else {
            readObject();
        }
        File file1 = new File(FILE_PATH1);
        if (0 == file1.length()) {
            queMap = new LinkedHashMap<>();
            Question q1 = new Question(1, "1+1=", "A.1", "B.2", "C.3", "D.4", "B");
            Question q2 = new Question(2, "1+2=", "A.1", "B.2", "C.3", "D.4", "C");
            Question q3 = new Question(3, "1+3=", "A.1", "B.2", "C.3", "D.4", "D");
            Question q4 = new Question(4, "1+4=", "A.1", "B.2", "C.3", "D.5", "D");
            Question q5 = new Question(5, "1+5=", "A.6", "B.2", "C.3", "D.5", "A");
            queMap.put(q1.getqId(), q1);
            queMap.put(q2.getqId(), q2);
            queMap.put(q3.getqId(), q3);
            queMap.put(q4.getqId(), q4);
            queMap.put(q5.getqId(), q5);
            writeQueObject();
        } else {
            readQueObject();
        }
    }

    /**
     * 自定义成员方法，实现管理员的登录
     * @param user
     * @return
     */
    public boolean serverManageCheck(User user) {
        if ("admin".equalsIgnoreCase(user.getUserName()) && "123456".equals(user.getPassword())) {
            return true;
        }
        return false;
    }

    /**
     * 自定义成员方法，实现学生的登录
     * @param id
     * @param stuName
     * @param stuPassword
     * @return
     */
    public boolean serverStuCheck(int id, String stuName, String stuPassword) {
        if (stuMap.containsKey(id)) {
            // 根据学号获取到用户名和密码
            // 学号，姓名，密码都正确的情况下，登录成功，否则登录失败
            String checkPassword = stuMap.get(id).getStuPassword();
            String checkStuName = stuMap.get(id).getStuName();
            if (checkPassword.equals(stuPassword) && checkStuName.equals(stuName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 自定义成员方法，实现学生信息的写入
     */
    public void writeObject() {
        OutputStream out = null;
        try {
            out = new FileOutputStream(FILE_PATH);
            oos = new ObjectOutputStream(out);
            oos.writeObject(stuMap);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 自定义成员方法，实现考题的写入
     */
    public void writeQueObject() {
        OutputStream out = null;
        try {
            out = new FileOutputStream(FILE_PATH1);
            oos = new ObjectOutputStream(out);
            oos.writeObject(queMap);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 自定义成员方法，实现学生信息的读取
     */
    public void readObject() {
        InputStream ins = null;
        try {
            ins = new FileInputStream(FILE_PATH);
            ois = new ObjectInputStream(ins);
            stuMap = (LinkedHashMap<Integer, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != ins) {
                    ins.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 自定义成员方法，实现考题的读取
     */
    public void readQueObject() {
        InputStream ins = null;
        try {
            ins = new FileInputStream(FILE_PATH1);
            ois = new ObjectInputStream(ins);
            queMap = (LinkedHashMap<Integer, Question>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != ins) {
                    ins.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 自定义成员方法，实现学生的添加
     * @param stu
     * @return
     */
    public boolean addStu(Student stu) {
        // 学号已经存在，不能重复添加
        if (stuMap.containsKey(stu.getId())) {
            return false;
        } else {
            stuMap.put(stu.getId(), stu);
            writeObject();
            return true;
        }
    }
    public boolean addQuestion(Question que) {
        // 题号重复，不能添加
        if (queMap.containsKey(que.getqId())) {
            return false;
        } else {
            queMap.put(que.getqId(), que);
            writeQueObject();
            return true;
        }
    }
    /**
     * 自定义成员方法，实现学员密码的修改
     * @param id
     * @param newPassword
     * @return
     */
    public boolean updateStuPass(int id, String newPassword) {
        // 根据学号修改学员密码
        if (stuMap.containsKey(id)) {
            stuMap.get(id).setStuPassword(newPassword);
            writeObject();
            return true;
        }
        return false;
    }

    /**
     * 自定义成员方法，实现学员姓名的修改
     * @param id
     * @param newStuName
     * @return
     */
    public boolean updateStuName(int id, String newStuName) {
        // 根据学号修改学员姓名
        if (stuMap.containsKey(id)) {
            stuMap.get(id).setStuName(newStuName);
            writeObject();
            return true;
        }
        return false;
    }

    /**
     * 自定义成员方法，实现题目的修改
     * @param qId
     * @param rightAnswer
     * @return
     */
    public boolean updateQueAnswer(int qId, String rightAnswer) {
        if (queMap.containsKey(qId)) {
            queMap.get(qId).setRightAnswer(rightAnswer);
            writeQueObject();
            return true;
        }
        return false;
    }
    public boolean removeQue(int qId) {
        if (queMap.containsKey(qId)) {
            queMap.remove(qId);
            writeQueObject();
            return true;
        }
        return false;
    }
    /**
     * 自定义成员方法，实现学员的删除
     * @param id
     * @return
     */
    public boolean removeStu(int id) {
        if (stuMap.containsKey(id)) {
            stuMap.remove(id);
            writeObject();
            return true;
        }
        return false;
    }

    /**
     * 自定义成员方法，实现考试题目的查询
     */
    public void showQue() {
        for (Integer key : queMap.keySet()) {
            System.out.println(queMap.get(key));
        }
    }

    /**
     * 自定义成员方法，实现考试功能
     * @param stuId
     */
    public void startTest(int stuId) {
        int score = 0;
        int rightQue = 0;
        int errQue = 0;
        for (Integer key : queMap.keySet()) {
            System.out.println("第" + key + "题: " + queMap.get(key).getqWords());
            System.out.println(queMap.get(key).getAnswer1());
            System.out.println(queMap.get(key).getAnswer2());
            System.out.println(queMap.get(key).getAnswer3());
            System.out.println(queMap.get(key).getAnswer4());
            System.out.println("请输入你的选项: ");
            String choice = ScannerUtil.scannerOpen().next();
            if (choice.equalsIgnoreCase(queMap.get(key).getRightAnswer())) {
                score += 20;
                rightQue++;
            } else {
                errQue++;
            }
        }
        qs.setStuId(stuId);
        qs.setScore(score);
        qs.setRightQue(rightQue);
        qs.setErrQue(errQue);
        System.out.println("考试结束");
    }
    public void checkTest() {
        System.out.println("学号: " + qs.getStuId());
        System.out.println("姓名: " + stuMap.get(qs.getStuId()).getStuName());
        System.out.println("正确题目: " + qs.getRightQue() + "题");
        System.out.println("错误: " + qs.getErrQue() + "题");
        System.out.println("得分: " + qs.getScore());
    }
    /**
     * 自定义成员方法，实现所有学员信息的查询
     */
    public void showStu() {
        System.out.println("******学  员  信  息******");
        System.out.println("学号" + "\t" + "用户名" + "\t" + "用户密码");
        for (Integer key : stuMap.keySet()) {
            System.out.println(stuMap.get(key).getId() + "\t" + stuMap.get(key).getStuName() + "\t" + stuMap.get(key).getStuPassword());
        }
        System.out.println("\n\n");
    }

}
